package com.gyh.stlserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.entity.Course;
import com.gyh.stlserver.entity.Member;
import com.gyh.stlserver.entity.Reserve;
import com.gyh.stlserver.mapper.CourseMapper;
import com.gyh.stlserver.mapper.MemberMapper;
import com.gyh.stlserver.mapper.ReserveMapper;
import com.gyh.stlserver.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.VarHandle;
import java.util.Date;
import java.util.List;

@Service
public class ReserveServiceImpl implements ReserveService {
    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Reserve> getReserveByMid(Integer mid) {
        return reserveMapper.selectReserveByMid(mid);
    }

    @Override
    public PageInfo<Reserve> getAll(Integer pageNum, Integer pageSize, Reserve reserve) {
        PageHelper.startPage(pageNum,pageSize);
        List<Reserve> reserves = reserveMapper.select(reserve);
        return new PageInfo<>(reserves);
    }

    @Override
    public int add(Reserve reserve) {
        /*1.判断会员是否已经预约该课程*/
        Reserve dbReserve = reserveMapper.selectReserveByMidAndCid(reserve);
        if (ObjectUtil.isNotEmpty(dbReserve)) {
            throw new RuntimeException("会员已经预约该课程");
        }
        /*2.判读会员是否存在*/
        Member member = memberMapper.selectMemberById(reserve.getMemberId());
        if (ObjectUtil.isEmpty(member)) {
            throw new RuntimeException("会员不存在");
        }
        /*3.判读课程是否存在*/
        Course course = courseMapper.selectCourseById(reserve.getCourseId());
        if (ObjectUtil.isEmpty(course)) {
            throw new RuntimeException("课程不存在");
        }
        /*4.上课后不能预约*/
        Date now = new Date();
        Date courseTime = course.getTime();
        Date endTime = new Date(courseTime.getTime() + course.getLength()*60*1000);
        /*5.课程已经结束*/
        if (now.after(endTime)){
            throw new RuntimeException("课程已经结束");
        }
        if (now.after(courseTime)&&now.before(endTime)){
            throw new RuntimeException("课程正在上课，无法预约");
        }
        /*6.开课前一个小时之内，才能预约*/
        if (now.before(courseTime)&&now.after(new Date(courseTime.getTime()-60*60*1000))){
            reserve.setReserveTime(new Date());
            reserve.setStatus(1);
           return reserveMapper.insert(reserve);
        }else {
            throw new RuntimeException("必须在开课的前一个小时内预约");
        }
    }

    @Override
    public int edit(Reserve reserve) {
        /*1.判断会员是否已经预约该课程*/
        Reserve dbReserve = reserveMapper.selectReserveByMidAndCid(reserve);
        if (ObjectUtil.isNotEmpty(dbReserve)) {
            /*取消预约*/
            if (reserve.getStatus().equals(0)){
                return reserveMapper.update(reserve);
            }
            throw new RuntimeException("会员已经预约该课程");
        }
        /*2.判读会员是否存在*/
        Member member = memberMapper.selectMemberById(reserve.getMemberId());
        if (ObjectUtil.isEmpty(member)) {
            throw new RuntimeException("会员不存在");
        }
        /*3.判读课程是否存在*/
        Course course = courseMapper.selectCourseById(reserve.getCourseId());
        if (ObjectUtil.isEmpty(course)) {
            throw new RuntimeException("课程不存在");
        }
        /*4.上课后不能预约*/
        Date now = new Date();
        Date courseTime = course.getTime();
        Date endTime = new Date(courseTime.getTime() + course.getLength());
        if (now.after(courseTime)&&now.before(endTime)){
            throw new RuntimeException("课程正在上课，无法预约");
        }
        /*5.课程已经结束*/
        if (now.after(endTime)){
            throw new RuntimeException("课程已经结束");
        }
        /*6.开课前一个小时之内，才能预约*/
        if (now.before(courseTime)&&now.after(new Date(courseTime.getTime()-60*60*1000))){
            reserve.setReserveTime(now);
            return reserveMapper.update(reserve);
        }else {
            throw new RuntimeException("必须在开课的前一个小时内预约");
        }
    }

    @Override
    public int attendance(Integer cid, Integer mid) {
        Reserve reserve = new Reserve();
        reserve.setCourseId(cid).setMemberId(mid);
        Reserve reserve1 = reserveMapper.selectReserveByMidAndCid(reserve);
        /*课程完成不能签到*/
        Course course = courseMapper.selectCourseById(cid);
        if (ObjectUtil.isEmpty(course)){
            throw new RuntimeException("课程不存在");
        }
        Date endTime = new Date(course.getTime().getTime()+course.getLength()*60*1000);
        Date now = new Date();
        if (now.after(endTime)){
            throw new RuntimeException("课程已结束，不能签到");
        }
        if (ObjectUtil.isEmpty(reserve1)){
            throw new RuntimeException("未查到预约课程");
        }
        if (reserve1.getStatus()!=1){
            throw new RuntimeException("已签到");
        }
        reserve1.setStatus(4);
        return reserveMapper.update(reserve1);
    }

    @Override
    public int cancelReserve(Reserve reserve) {
        if (ObjectUtil.isEmpty(reserve)){
            throw new RuntimeException("预约不存在");
        }
        Course course = courseMapper.selectCourseById(reserve.getCourseId());
        if (ObjectUtil.isEmpty(course)) {
            throw new RuntimeException("课程不存在");
        }
        Date endTime = new Date(course.getTime().getTime()+course.getLength()*60*1000);
        Date now = new Date();
        if (now.after(endTime)){
            throw new RuntimeException("已完课，不能取消预约");
        }
        reserve.setStatus(0);
        return reserveMapper.update(reserve);
    }

    @Override
    public int removeByCidAndMid(Integer mid, Integer cid) {

        return 0;
    }


}
